Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Partial Class TheatreStaff_SessionDates
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		srcgrdSession.SelectParameters("username").DefaultValue = Page.User.Identity.Name
		'srcgrdSchedule.SelectParameters["username"].DefaultValue = Page.User.Identity.Name;
		'grdSpecialTickets.Sort("MovieTitle", SortDirection.Ascending);
		Dim connectionString As String
		connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

		Dim con As New SqlConnection(connectionString)
		Dim cmd As New SqlCommand("SELECT TheatreId from TheatreStaff WHERE TheatreStaffUsername=@username", con)

		cmd.Parameters.AddWithValue("@username", Page.User.Identity.Name)

		Using con
			con.Open()
			Dim TheatreID As Object = cmd.ExecuteScalar()
			If TheatreID IsNot Nothing Then
				lblId.Text = TheatreID.ToString()
			End If
		End Using
		Dim con1 As New SqlConnection(connectionString)
		Dim cmd1 As New SqlCommand("SELECT Theatrename from Theatre WHERE Theatreid=@theatreid", con1)

		cmd1.Parameters.AddWithValue("@theatreid", lblId.Text)


		Using con1
			con1.Open()
			Dim Theatrename As Object = cmd1.ExecuteScalar()

			If Theatrename IsNot Nothing Then

				Label5.Text = Theatrename.ToString()
			End If
		End Using

    End Sub
    Protected Sub Clear(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = grdSession.SelectedRow
        Label7.Text = row.Cells(3).Text
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub

    Protected Sub Cancel(ByVal sender As Object, ByVal e As EventArgs)

        'txtDateI.Text = string.Empty;
        'ddlMovieTitleI.SelectedIndex = -1;
        'ddlscheduleI.SelectedIndex = -1;
        'ddlSessionI.SelectedItem.Text = "Select";
        'ddlTypeI.SelectedIndex = -1;
        'ddlCinema.SelectedIndex = -1;
        Label15.Text = String.Empty
        Label16.Text = String.Empty
        grdSession.DataBind()
    End Sub

    'protected void Insert(object sender, EventArgs e)
    '{
    '    Label15.Text = string.Empty;
    '    Label16.Text = string.Empty;
    '    string connectionString;
    '    connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    '    SqlConnection con = new SqlConnection(connectionString);
    '    SqlConnection con1 = new SqlConnection(connectionString);
    '    SqlCommand cmd1 = new SqlCommand("INSERT datesession(date,sessionid,movieid,sessiontype,scheduleid) values (@date,@sessionid,@movieid,@sessiontype,@scheduleid)", con1);
    '    SqlCommand cmd = new SqlCommand("SELECT datesession.date,datesession.sessionid from datesession,Movie,TheatreStaff WHERE TheatreStaff.TheatreId=Movie.TheatreId AND datesession.movieid=Movie.MovieId AND Movie.TheatreId=@TheatreId AND datesession.date=@date AND datesession.sessionid=@sessionid", con);
    '    string ParamString = lblId.Text;
    '    Int32 RowCount;
    '    Boolean HasMovies;

    '    cmd.Parameters.AddWithValue("@TheatreId", ParamString);
    '    cmd.Parameters.AddWithValue("@date", txtDateI.Text );
    '    cmd.Parameters.AddWithValue("@sessionid", ddlSessionI.SelectedValue);
    '    //cmd.Parameters.AddWithValue("@movieid", ddlMovieTitleI.SelectedValue );

    '    using (con)
    '    {
    '        con.Open();
    '        SqlDataReader Reader = cmd.ExecuteReader();

    '        if (Reader.HasRows)
    '        {

    '            Label15.Text = "The session date " + txtDateI.Text + " already exist in the database!";

    '            //lblCheck.Text = string.Empty;
    '            //ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "Focus", "<script language='JavaScript' 'showInsert();'/>");

    '            HasMovies = true;

    '        }
    '        else
    '        {
    '            HasMovies = false;

    '        }
    '    }
    '    using (con1)
    '    {
    '        con1.Open();
    '        if (HasMovies == false)
    '        {
    '            cmd1.Parameters.AddWithValue("@date", txtDateI.Text);
    '            cmd1.Parameters.AddWithValue("@sessionid", ddlSessionI.SelectedValue );
    '            cmd1.Parameters.AddWithValue("@movieid", ddlMovieTitleI.SelectedValue );
    '            cmd1.Parameters.AddWithValue("@sessiontype", ddlTypeI.SelectedValue );
    '            cmd1.Parameters.AddWithValue("@scheduleid", ddlscheduleI.SelectedValue );
    '            RowCount = cmd1.ExecuteNonQuery();
    '            txtDateI .Text  = string.Empty;
    '            ddlMovieTitleI.SelectedIndex = -1;
    '            ddlscheduleI.SelectedIndex = -1;
    '            //ddlSessionI.SelectedItem.Text = "Select";
    '            ddlTypeI.SelectedIndex = -1;
    '            ddlCinema.SelectedIndex = -1;
    '            //if (RowCount > 0)
    '            //{
    '            Label16.Text  = "The session " + txtDateI.Text  + " has been added sucessfully!";


    '            //}
    '        }

    '    }
    '    grdSession.DataBind();
    '}
    Private Sub Page_Load()
        If Not Page.IsPostBack Then
            grdSession.SelectedIndex = 0
        End If
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
    Protected Sub OnItemInserted_Click(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs)
        grdSession.DataBind()
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
    Protected Sub OnItemUpdated_Click(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
        grdSession.DataBind()
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub
    Protected Sub function1(ByVal sender As Object, ByVal e As EventArgs)
        Dim tb As DropDownList = DirectCast(dtlSpecial.FindControl("ddlSession"), DropDownList)
        Label3.Text = tb.SelectedItem.Value
    End Sub
    Protected Sub function2(ByVal sender As Object, ByVal e As EventArgs)
        Dim tb1 As DropDownList = DirectCast(dtlSpecial.FindControl("ddlMovieTitle"), DropDownList)
        Label4.Text = tb1.SelectedItem.Text
    End Sub

    Protected Sub function3(ByVal sender As Object, ByVal e As EventArgs)
        Dim tb2 As TextBox = DirectCast(dtlSpecial.FindControl("txtSessionDate"), TextBox)
        Label6.Text = tb2.Text
    End Sub

    Protected Sub function4(ByVal sender As Object, ByVal e As EventArgs)
        Dim tb3 As DropDownList = DirectCast(dtlSpecial.FindControl("ddlType"), DropDownList)


    End Sub

    Protected Sub function6(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = grdSession.SelectedRow
        Label7.Text = row.Cells(2).Text
    End Sub

    Protected Sub function5(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        Dim tb As DropDownList = DirectCast(dtlSpecial.FindControl("ddlSession"), DropDownList)
        Dim tb1 As DropDownList = DirectCast(dtlSpecial.FindControl("ddlMovieTitle"), DropDownList)
        Dim tb2 As TextBox = DirectCast(dtlSpecial.FindControl("txtSessionDate"), TextBox)
        Dim tb3 As DropDownList = DirectCast(dtlSpecial.FindControl("ddlType"), DropDownList)

        If (Label3.Text = tb.SelectedItem.Value) AndAlso (Label6.Text = tb2.Text) Then
            args.IsValid = True
            Label8.Text = "same"
        Else


            Dim connectionString As String
            connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim con As New SqlConnection(connectionString)
            Dim cmd As New SqlCommand("SELECT datesession.date,datesession.sessionid from datesession,Movie,TheatreStaff WHERE   datesession.date=@date AND datesession.sessionid=@sessionid", con)
            Dim ParamString As String = lblId.Text


            cmd.Parameters.AddWithValue("@date", DateTime.Parse(tb2.Text))
            cmd.Parameters.AddWithValue("@sessionid", tb.SelectedItem.Value)
            'cmd.Parameters.AddWithValue("@movieid", ddlMovieTitleI.SelectedValue );

            Using con
                con.Open()
                Dim Reader As SqlDataReader = cmd.ExecuteReader()

                If Reader.HasRows Then
                    args.IsValid = False

                    Label8.Text = " different"
                Else

                    args.IsValid = True
                    Label8.Text = " new"
                End If
            End Using
        End If
    End Sub
End Class
